/*
 * cms登录Ajax:
 * 与后端的交互: 除内部错误500外其他情况为200
 * 200后解析数据
 * 后端返回String: code#content,  code is int, content is string
 * 注册成功返回"0#成功提示", 注册失败返回"1#失败原因"
 */
$("#cmsLoginButton").click(function(){
	var username = $("input[id='m_username']").val();
	var password = $("input[id='m_password']").val();

    //正则表达式先检验一波格式
    if (!IsFullyMatch("^[a-zA-Z0-9_]{8,16}$", username) || 
    	!IsFullyMatch("^[a-zA-Z][a-zA-Z0-9_]{7,15}$", password)){
		$("#login_hidden_info").text("格式错误");
		$("#login_hidden_info").show();
    	return;
    }

	var xmlhttp;
	if (window.XMLHttpRequest)
	{
		// code for IE7+, Firefox, Chrome, Opera, Safari
		xmlhttp=new XMLHttpRequest();
	 }
	else
	{	// code for IE6, IE5
		xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
	}
	xmlhttp.onreadystatechange=function()
	{
		if (xmlhttp.readyState==4)
		{
		    if (xmlhttp.status==200)
			{
				var response = xmlhttp.responseText.split("#");
				var code = parseInt(response[0])
				var str = response[1]
				if (code == 0)
				{
				    $("#login_hidden_info").text(str + "3秒后跳转到CMS首页");
				    $("#login_hidden_info").show();
				    window.setTimeout("window.location='/cms/index.html'", 3000);
				}
			    else
				{
				    $("#login_hidden_info").text(str);
				    $("#login_hidden_info").show();
				}
			}
			else
			{
				$("#login_hidden_info").text("内部错误");
				$("#login_hidden_info").show();
			}
		}
	}
	xmlhttp.open("POST","/cms/loginPOST",true);
	xmlhttp.setRequestHeader("Content-type","application/x-www-form-urlencoded");
	xmlhttp.send(JSON.stringify(
		{"username": username, "password": md5(password)
	}));
});
